
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <regex.h>
#include <errno.h>
 /*
 * Match string against the extended regular expression in
 * pattern, treating errors as no match.
 *
 * return 1 for match, 0 for no match
 */

void match(const char *string, char *pattern)
{
    int status;
    regex_t re;
    regmatch_t match[3];
    int index=0;
    char *p = (char *)malloc(strlen(string)+1);
    if (regcomp(&re, pattern, REG_EXTENDED) != 0) 
    {
        printf("regcomp error %d", errno);
        return;      /* report error */
    }
    
    status = regexec(&re, string+index, (size_t)3, &match[0], 0);
    index=match[0].rm_eo;
    status = regexec(&re, string+index, (size_t)3, &match[0], 0);
    
    printf("status = %d\n", status);
    /*
    if (status == 0)
    {
        memcpy(p, string+match[0].rm_so, match[0].rm_eo-match[0].rm_so);
        p[match[0].rm_eo-match[0].rm_so] = 0;
        printf("%s\n",p);
        memcpy(p, string+match[1].rm_so, match[1].rm_eo-match[1].rm_so);
        p[match[1].rm_eo-match[1].rm_so] = 0;
        printf("%s\n",p);
    }
    else
    {
        printf("regexec error \n");
    }
    */
    regfree(&re);
    free(p);    
}

int main()
{    
    //match("[abcd]", "^\\b*[\\[]{[^\\[\\]]*}[\\]]\\b*$");
    //match("[abcd]", "^\\b*[{[^\\[\\]]*}]\\b*$");
    //match("[abcd]", "^\\s*\\[(.*)\\]$");
    //match("aaa=111", "^([^\\=]+)=?(.*)$");
    //match("[abcd][1234]", "\\[(.*)\\]");
    //match("[abcd][1234]", "[\\[][^\\[\\]](.*)[\\]]");
    match("abaa", "(a)");
    match("aaaa", "b");
    return 0;    
}

